var globalServerURL = 'http://localhost/debatepapo/';
//var globalServerURL = 'http://www.huhul.com.br/debatepapo/';
var globalShadingOpened = false;

window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
	appId      : '334718206628813',                        // App ID from the app dashboard      
	status     : true,                                 // Check Facebook Login status
	xfbml      : true                                  // Look for social plugins on the page
	});
	// channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel file for x-domain comms
	// Additional initialization code such as adding Event Listeners goes here
};

// Load the SDK asynchronously
(function(d, s, id){
	var js, fjs = d.getElementsByTagName(s)[0];
	if (d.getElementById(id)) {return;}
	js = d.createElement(s); js.id = id;
	js.src = "//connect.facebook.net/en_US/all.js";
	fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

function logout(){
	var com = new Communicator();
	com.addParameter('type','user');
	com.addParameter('task','logout');
	com.sendRequest(logoutHandler);
}

function logoutHandler(res){
	loadUrl(res);
}

function loadPage(){
	global = new Global();
	
	if(typeof startTasks === 'function') startTasks();
}

function ownedGroupsHandler(ownedGroups){
	var fragment = document.createDocumentFragment();

	for(var i=0, iMax=ownedGroups.length; i<iMax; i++){
		var current = ownedGroups[i];

		var group = new Group();
		group.assignObject(current);

		fragment.appendChild(group.createOwnGroupItem());
	}

	document.getElementById('ownedGroups').appendChild(fragment);
}

function openedRoomsHandler(openedRooms){
	var fragment = document.createDocumentFragment();

	for(var i=0, iMax=openedRooms.length; i<iMax; i++){
		var current = openedRooms[i];

		var group = new Group();
		group.assignObject(current);

		var room = new Room(group);

		fragment.appendChild(room.createOpenedRoomItem());
	}

	document.getElementById('openedRooms').appendChild(fragment);
}

function roomMembersHandler(roomMembers){
	var fragment = document.createDocumentFragment();

	for(var i=0, iMax=roomMembers.length; i<iMax; i++){
		var current = roomMembers[i];
		
		var user = new User(current['id']);
		user.assignProperties(current['name'], current['administrator'], current['status']);

		fragment.appendChild(user.createRoomMemberItem());
	}

	document.getElementById('users').appendChild(fragment);
}

function handleShading(event, type){
	var caller = null;

	if(!event && !type)	caller = document.getElementById('shading');	
	else caller = event.target;

	var func = null;
	if(caller.id == 'shading'){
		addClass(caller, 'invisible');
		preventScrolling(false);

		func = window['close'+captiliseFirstLetter(globalShadingOpened)];
	
		var sections = caller.getElementsByClassName('section');
		for(var i=0, iMax=sections.length; i<iMax; i++) addClass(sections[i], 'invisible');

		globalShadingOpened = false;
	}else{ 
		if(!type) return true;
		
		func = window['open'+captiliseFirstLetter(type)];
		var shading = document.getElementById('shading');
		removeClass(shading, 'invisible');
		preventScrolling(true);
		removeClass(document.getElementById(type), 'invisible');

		globalShadingOpened = type;
	}

	if(typeof func === 'function') func();
}